const request=require('../../utils/request.js');//获取封装的请求函数
const globleurl = require('../../utils/serverconfig.js').url;
const formatTime=require('../../utils/formattime').formatTime;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cinemaid:0,
    cinemainfo:{},
    movielist:[],
    today:'',
    tomorrow:'',
    // currentdayid:0,
    currentmovieid:0,
    todaylist:[],
    tomorrowlist:[],
    nodataurl:globleurl+'/images/nodata.png',
    shownodata:false
  },
  

  onLoad: function (options) {
  
    let cinemaid=parseInt(options.cinemaid);
    
    this.setData({
     cinemaid
    });
    this.getDate();

    this.getcinemaInfo();

    this.daymovielist(3)

    
    // this.getdaydesc()
  },

  // 切换热映待映
  toogleplay(e){
  if(e.detail.name=='a'){

    this.daymovielist(3)
  }else{

    this.daymovielist(2);
  }
  },

  async getcinemaInfo(){
    let cinemainfo=await request.myrequest('/miniapp/getcinemaInfo',{cinemaid:this.data.cinemaid});
    let cinemanm=cinemainfo.data[0].nm
    this.setData({
      cinemainfo:cinemainfo.data,
      cinemanm
    });
    wx.setNavigationBarTitle({
      title: `${cinemanm}`
    })
  },

  //获取当前热映或待映电影列表
  async daymovielist(playstate){
    let moviedata=await request.myrequest('/miniapp/daymovielist',{playstate,cinemaid:this.data.cinemaid,today:this.data.today});
  
    //如果没有电影信息
    if(moviedata.data.length==0){
      this.setData({shownodata:true})
      return 
    }
    let movielist=moviedata.data;
    
    movielist.forEach((item)=>{
      item.img_url=globleurl+item.img_url;
    })
    let movieid=movielist[0].movie_id;
    this.setData({
      movielist,
      // currentdayid:movielist[0].day_id, //默认获取到列表后将当前档期id存储,
      currentmovieid:movieid
    });
  
      //默认将下方电影信息渲染成的第一条电影
     
    this.getmovieinfo(movieid)
    this.getdaydesc(0);
  },

  //获取电影详情
  async getmovieinfo(movieid){
    let movieinfo=await request.myrequest("/miniapp/movie",{movie_id:movieid});
    this.setData({
      movieinfo:movieinfo.data[0],
      // currentmovieid:movieid
    });
  },


//获取当前日期
  getDate(){
    let nowtime=new Date();
    //今日
    let todaymonth=nowtime.getMonth()+1;
    todaymonth=todaymonth>=10?todaymonth:'0'+todaymonth;
    let todaydate=nowtime.getDate();
    todaydate=todaydate>=10?todaydate:'0'+todaydate;
    let today=todaymonth+'-'+todaydate;

    //明日
    let tomonth=nowtime.getMonth()+1;
    tomonth=tomonth>=10?tomonth:'0'+tomonth;//明日月
    let todate=nowtime.getDate()+1;
    todate=todate>=10?todate:'0'+todate;
    let tomorrow=tomonth+'-'+todate;
    
    this.setData({
      today,
      tomorrow
    })
  },


  //选择电影
  selectmovie(e){
    let movieid=e.currentTarget.dataset.movieid;
    this.setData({
      currentmovieid:movieid
    });
    this.getmovieinfo(movieid);
    this.getdaydesc(0);

  },

  //点击切换今明两天
  clicktime(e){
    if(e.detail.index==0){
      this.getdaydesc(0);
    }else{
      this.getdaydesc(1);
    }
  },

  // 点击购票
  buytiket(e){

    let day_id=e.currentTarget.dataset.day_id;
    let index=e.currentTarget.dataset.index;
    let cinemanm=this.data.cinemanm;
    wx.navigateTo({
      url: `../selectseat/movies/movies?day_id=${day_id}&&index=${index}&&cinemanm=${cinemanm}`,
    })
  },

  //点击location
  ontaplocation(){
  wx.navigateTo({
    url: `./component/map/map?cinemaid=${this.data.cinemaid}`, 
  })
  },

  //获取档期详情
  async getdaydesc(time){
    //0是今天，1是明天
    if(time==0){
    let dayinfo=await request.myrequest("/miniapp/getdayarrange",{cinema_id:this.data.cinemaid,movie_id:this.data.currentmovieid,date:this.data.today});
    this.setData({
        todaylist:dayinfo.data
      });

    }else{
      let dayinfo=await request.myrequest("/miniapp/getdayarrange",{cinema_id:this.data.cinemaid,movie_id:this.data.currentmovieid,date:this.data.tomorrow});
      this.setData({
          tomorrowlist:dayinfo.data
        });
    }
  }

})